package demoMod.moduploader.model;

import com.badlogic.gdx.Gdx;
import com.codedisaster.steamworks.*;
import demoMod.gdxform.core.FormManager;
import demoMod.gdxform.helpers.FontHelper;
import demoMod.moduploader.fonts.FontKeys;
import demoMod.moduploader.localization.LocalizedStrings;
import demoMod.moduploader.ui.MessageBox;
import demoMod.moduploader.ui.ModDisplayItem;
import demoMod.moduploader.ui.ModUploadWindow;

import java.util.List;

public class ModUploadCallback implements SteamUGCCallback {
    private final ModDisplayItem modDisplayItem;
    private boolean isUpdate = true;

    public ModUploadCallback(ModDisplayItem modDisplayItem) {
        this.modDisplayItem = modDisplayItem;
    }

    @Override
    public void onUGCQueryCompleted(SteamUGCQuery steamUGCQuery, int i, int i1, boolean b, SteamResult steamResult) {

    }

    @Override
    public void onSubscribeItem(SteamPublishedFileID steamPublishedFileID, SteamResult steamResult) {

    }

    @Override
    public void onUnsubscribeItem(SteamPublishedFileID steamPublishedFileID, SteamResult steamResult) {

    }

    @Override
    public void onRequestUGCDetails(SteamUGCDetails steamUGCDetails, SteamResult steamResult) {

    }

    private Long hexToLong(String s) {
        return Long.parseLong(s.trim(), 16);
    }

    @Override
    public void onCreateItem(SteamPublishedFileID publishedFileID, boolean needsToAcceptWLA, SteamResult result) {
        List<String> strings = LocalizedStrings.getStrings("ModInfoWindow");
        ModUploadWindow modUploadWindow = (ModUploadWindow) FormManager.getInstance().getContainerById(ModUploadWindow.ID);
        if (needsToAcceptWLA) {
            new MessageBox("", strings.get(19), FontHelper.getFont(FontKeys.SIM_HEI_14)).setConfirmCallback(() -> Gdx.net.openURI("https://steamcommunity.com/sharedfiles/workshoplegalagreement"));
            modUploadWindow.closeLater();
            return;
        }
        isUpdate = false;
        if (result == SteamResult.OK) {
            this.modDisplayItem.getModInfoCache().steamPublishedID = hexToLong(publishedFileID.toString()).toString();
            this.modDisplayItem.getModUploadItem().saveConfigFile(this.modDisplayItem.getModInfoCache());
            modUploadWindow.updateItemInfo(this.modDisplayItem);
        } else {
            modUploadWindow.closeLater();
            modUploadWindow.changeStatusText(strings.get(17) + result.name());
        }
    }

    @Override
    public void onSubmitItemUpdate(SteamPublishedFileID steamPublishedFileID, boolean needsToAcceptWLA, SteamResult result) {
        List<String> strings = LocalizedStrings.getStrings("ModInfoWindow");
        ModUploadWindow modUploadWindow = (ModUploadWindow) FormManager.getInstance().getContainerById(ModUploadWindow.ID);
        if (needsToAcceptWLA) {
            new MessageBox("", strings.get(19), FontHelper.getFont(FontKeys.SIM_HEI_14)).setConfirmCallback(() -> Gdx.net.openURI("https://steamcommunity.com/sharedfiles/workshoplegalagreement"));
            modUploadWindow.closeLater();
            return;
        }
        if (result == SteamResult.OK) {
            modUploadWindow.closeLater();
            modUploadWindow.changeStatusText(strings.get(isUpdate ? 16 : 15));
        } else {
            modUploadWindow.closeLater();
            modUploadWindow.changeStatusText(strings.get(17) + result.name());
        }
    }

    @Override
    public void onDownloadItemResult(int i, SteamPublishedFileID steamPublishedFileID, SteamResult steamResult) {

    }

    @Override
    public void onUserFavoriteItemsListChanged(SteamPublishedFileID steamPublishedFileID, boolean b, SteamResult steamResult) {

    }

    @Override
    public void onSetUserItemVote(SteamPublishedFileID steamPublishedFileID, boolean b, SteamResult steamResult) {

    }

    @Override
    public void onGetUserItemVote(SteamPublishedFileID steamPublishedFileID, boolean b, boolean b1, boolean b2, SteamResult steamResult) {

    }

    @Override
    public void onStartPlaytimeTracking(SteamResult steamResult) {

    }

    @Override
    public void onStopPlaytimeTracking(SteamResult steamResult) {

    }

    @Override
    public void onStopPlaytimeTrackingForAllItems(SteamResult steamResult) {

    }

    @Override
    public void onDeleteItem(SteamPublishedFileID steamPublishedFileID, SteamResult steamResult) {

    }
}
